package cn.icanci.ladybird.controller;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.pojo.dto.UserOrderDTO;
import cn.icanci.ladybird.pojo.entity.OrderInfo;
import cn.icanci.ladybird.service.user.UserConfirmOrderService;
import cn.icanci.ladybird.service.user.UserOrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author icanci
 * @author: YPF
 * @date: Created in 2020/10/4 14:42
 * @classAction: 用户订单
 */
@Controller
@RequestMapping("/api/v1/pri/user")
public class UserOrderController {

    @Autowired
    private UserOrderService userOrderService;
    @Autowired
    private UserErrorPagesController userErrorPagesController;
    @Autowired
    private UserConfirmOrderService userConfirmOrderService;

    @ResponseBody
    @RequestMapping("/userorder")
    public JsonData getUserOrder() {
        List<UserOrderDTO> userOrderDTOS = userOrderService.userOrderDTO();
        return JsonData.builderSuccess(userOrderDTOS);
    }

    @RequestMapping("/json/my-order")
    public String getUserOrderJson(Model model) {
        List<UserOrderDTO> userOrderDTOS = userOrderService.userOrderDTO();
        List<Object> list = new ArrayList<>();
        list.add(userOrderDTOS);
        JsonData jsonData = JsonData.builderSuccess(list);
        model.addAttribute("jsonData", jsonData);
        return "user/my-order";
    }

    @RequestMapping("/json/del-order")
    @ResponseBody
    public JsonData delUserOrder(Long orderInfoId) {
        OrderInfo orderInfo = userOrderService.updateByOrderInfoId(orderInfoId);
        if (null != orderInfo && orderInfo.getOrderInfoStatus() == 3) {
            return JsonData.builderSuccess();
        } else {
            return JsonData.builderError("网络异常，删除失败");
        }
    }

    /**
     * 跳转到 order-list 订单列表
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/order")
    public String adminTicketList(Model model,HttpServletRequest request, Integer pageNum, Integer pageSize) {
        // 页面参数，然后在Service进行分页
        Long userId = Long.valueOf(request.getSession().getAttribute("user_id").toString());
        PageInfo<UserOrderDTO> ticketDetailsPageInfo = userOrderService.queryAllByNotObject(userId, pageNum, pageSize);
        JsonData jsonData = JsonData.builderSuccess(ticketDetailsPageInfo);
        model.addAttribute("jsonData", jsonData);
        return "user/my-order";
    }

    /**
     * * 用户确认订单的接口
     *
     * @param list         处理座位模型
     * @param detailsId    商品id
     * @param cityAndVenue 城市和场馆
     * @param request      HttpServletRequest请求对象
     * @return 返回页面模型
     */
    @PostMapping("/json/confirm-order")
    @ResponseBody
    public JsonData confirmOrder(String[] list, Long detailsId, String cityAndVenue, HttpServletRequest request) {
        return userConfirmOrderService.confirmOrder(list, detailsId, cityAndVenue, request);
    }

    @GetMapping("/to-buy-page")
    public String userToBuyPage(Model model, HttpServletRequest request, Long orderInfoId) {
        Long userId = Long.valueOf(request.getSession().getAttribute("user_id").toString());
        JsonData jsonData = userConfirmOrderService.getBuyPageData(orderInfoId, userId);
        model.addAttribute("jsonData", jsonData);
        return "user/buy";
    }

    @ResponseBody
    @PostMapping("/to-buy-page-confirm")
    public JsonData userToBuyPageConfirm(String[] list, Long orderInfoId, Long ticketId, HttpServletRequest request) {
        // 处理数据
        JsonData afterConfirm = userConfirmOrderService.getBuyPageDataAfterConfirm(list, orderInfoId, ticketId);
        return afterConfirm;
    }

}
